#!/usr/bin/env python

import os
import shutil
import subprocess

class Environment:
    def __init__(self, build_path):
        self.build_path = build_path
        self.__setup__(self.build_path)
        self.__make__()

    def __setup__(self, build_path):
        if os.path.isdir(build_path):
            try:
                qclassify_path = str(build_path) + "/qclassify/"
                qclassify_svn = "/root/qasvn/scripts2/NAVL/qclassify/"
                if os.path.isdir(qclassify_svn):
                    shutil.copy(qclassify_svn + "qconfig.py", qclassify_path)
                    shutil.copy(qclassify_svn + "restart_qclassify.sh", qclassify_path)
                else:
                    print "Path does not exist: %s" % qclassify_svn
            except Exception, error_msg:
                print error_msg
        else:
            print "Path does not exist: %s" % build_path

    def __make__(self):
        capreader_path = self.build_path + "/capreader/"
        qclassify_path = self.build_path + "/qclassify/"
        make_clean_qclassify = "make clean -C " + qclassify_path
        make_clean_capreader = "make clean -C " + capreader_path
        make_qclassify = "make -C " + qclassify_path
        make_capreader = "make -C " + capreader_path
        chmod_qconfig = "chmod +x qconfig.py"
        chmod_re_qclassify = "chmod +x restart_qclassify.sh"
        if os.path.isdir(capreader_path):
            if os.path.isdir(qclassify_path):
                try:
                    subprocess.call(make_clean_capreader, shell=True)
                    subprocess.call(make_clean_qclassify, shell=True)
                    subprocess.call(make_capreader, shell=True)
                    subprocess.call(make_qclassify, shell=True)
                    subprocess.call(chmod_qconfig, cwd=qclassify_path, shell=True)
                    subprocess.call(chmod_re_qclassify, cwd=qclassify_path, shell=True)
                except Exception, error_msg:
                    print error_msg
            else:
                print "Path does not exist: %s" % qclassify_path
        else:
            print "Path does not exist: %s" % capreader_path 